home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- #include <ctype.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/signal.h>
- #include <sys/fcntl.h>
- #include <sys/errno.h>
- #include <string.h>
- #include <netinet/in.h>
- #include <sys/stat.h>
- #include <netdb.h>
-
- extern long sock;
-
- void createsocket(long portnum)
- {
- struct sockaddr_in saddr;
-
- /* Create a socket. */
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if(sock < 0) {
- perror("opening stream socket");
- exit(1);
- }
- if(sock == 0) {
- sock = dup((int)sock);
- }
- /* Construct name for parent socket on local machine using portnum. */
-
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = INADDR_ANY;
- saddr.sin_port = (unsigned short)htons(portnum);
- if(connect((int)sock, &saddr, sizeof(saddr)) < 0) {
- perror("connecting socket");
- exit(1);
- }
- }
-
- void sendpair(long dev, long val)
- {
- long buf[2];
-
- if (sock == -1) {
- printf("sendpair (%d,%d)\n",dev, val);
- return;
- }
- buf[0] = dev;
- buf[1] = val;
-
- if(write((int)sock, buf, sizeof(buf)) < 0) {
- perror("writing stream message");
- exit(1);
- }
- }
-
- void sendcmd(long dev)
- {
- sendpair(dev, 0);
- }
-
- void sendsocket(char *buf, long size)
- {
-
- if (sock == -1) {
- return;
- }
-
- if(write((int)sock, buf, (unsigned int)size) < 0) {
- perror("writing stream message");
- exit(1);
- }
- }
-
- long readbuf(int fd,char *p, int totbytes)
- {
- int nbytes, n;
-
- if (sock == -1) return 0;
- nbytes = 0;
- while (nbytes < totbytes) {
- if ( (n=read(fd,p+nbytes,totbytes-nbytes)) <= 0) {
- perror("gizmo read failure");
- exit(0);
- }
- nbytes += n;
- }
- return 0;
- }
-
- void readsocket(char *c, long size)
- {
- if (readbuf((int)sock,c,(int)size) < 0)
- exit(1);
- }
-
- long readlongfromsocket()
- {
- long val;
-
- if (readbuf((int)sock,(char *)&val,(int)sizeof(val)) < 0) {
- fprintf(stderr,"unknown data from main program\n");
- exit(0);
- }
- return val;
- }
-
- void sendascii(long type, char *str)
- {
- long buf[210], i = 2;
-
- if (sock == -1) return;
- buf[0] = type;
- buf[1] = -1;
- if (str != 0)
- while (*str) {
- buf[i++] = type;
- buf[i++] = *str++;
- }
- buf[i++] = type;
- buf[i++] = 0;
- if(write((int)sock, buf, (unsigned int)i*sizeof(long)) < 0) {
- perror("writing stream message");
- exit(1);
- }
- }
-